home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Bugs in My Serial / Bugs in My Serial code / ScreenMatrix.cpp < prev    next >
C/C++ Source or Header  |  1997-06-27  |  3KB  |  135 lines

  1. #include "ScreenMatrix.h"
  2. #include "MacsBugSerialStuff.h"
  3.  
  4.  
  5. void SMoveTo(short tY,short tX);
  6.  
  7. static void memset(Ptr inMem,char value,long inSize)
  8. {
  9.     for(long z = 0;z<inSize;z++){
  10.         inMem[z] = value;
  11.     }
  12. }
  13.  
  14. ScreenMatrix::ScreenMatrix()
  15. {    
  16.     memset((Ptr)fMatrix,' ',kWidth * kHeight);
  17.     
  18.     fLastScrollWriteCol = kFirstScrollCol;
  19.     fLastCommandWriteCol = kFirstScrollCol;
  20.     fInvalidQ = false;
  21.     fScrollCount = 0;
  22. }
  23.  
  24. void    ScreenMatrix::RefreshScrollArea()
  25. {
  26.     fInvalidQ = false;
  27.     fScrollCount = 0;            
  28.     long    numRows = ScrollAreaHeight();
  29.     for(long z = 1;z<=numRows;z++){
  30.         SMoveTo(z,1);
  31.         
  32.         long    theSize;
  33.         Ptr        theLine = GetIndRow(z,theSize);
  34.         
  35.         SerialDriver_SendBytes((unsigned char*)theLine,theSize);
  36.     }
  37. }
  38.  
  39. void    ScreenMatrix::Sync()
  40. {
  41.     if(fInvalidQ){
  42.         fSyncCount++;
  43.         if(fSyncCount > 10000){
  44.             RefreshScrollArea();
  45.         }
  46.     }
  47. }
  48.                     
  49.  
  50.  
  51. Boolean    ScreenMatrix::DrawChar(long inRow,long inCol,char inChar)
  52. {
  53.     if((inCol <= kWidth) && (inRow <= kHeight)){
  54.         Boolean            hadToScrollQ = false;
  55.         
  56.         if(inRow == kBottomScrollRow){
  57.             if(inCol >= kFirstScrollCol){
  58.                 if(inCol <= fLastScrollWriteCol){
  59.                     // need to scroll
  60.                     ScrollArea();
  61.  
  62.                     hadToScrollQ = true;
  63.                     fLastScrollWriteCol = kFirstScrollCol;
  64.                 }else{
  65.                     fLastScrollWriteCol = inCol;
  66.                 }
  67.             }
  68.             
  69.         }else if(inRow == kCommandRow){
  70.             if(inCol >= kFirstScrollCol){
  71.                 if(inCol <= fLastCommandWriteCol){
  72.                     // clear the line and send it
  73.                     memset(CalcStart(kFirstScrollCol,kCommandRow),' ',kWidth - (kFirstScrollCol-1));
  74.                     long    theSize;
  75.                     Ptr        theLine = GetIndRow(kCommandRow,theSize);
  76.                     
  77.                     SMoveTo(kCommandRow,1);
  78.                     SerialDriver_SendBytes((unsigned char*)theLine,theSize);
  79.                     fLastCommandWriteCol = kFirstScrollCol;
  80.                 }else{
  81.                     fLastCommandWriteCol = inCol;
  82.                 }
  83.             }
  84.         }
  85.  
  86.         fMatrix[inRow-1][inCol-1] = inChar;
  87.         
  88.         return hadToScrollQ;
  89.     }else{
  90.         return false;
  91.     }
  92. }
  93.  
  94. Ptr        ScreenMatrix::CalcStart(long inCol,long inRow)
  95. {
  96.     return &fMatrix[inRow-1][inCol-1];
  97. }
  98.  
  99. void    ScreenMatrix::CopyRow(long    fromRow,long toRow)
  100. {
  101.     Ptr        source = CalcStart(kFirstScrollCol,fromRow);
  102.     Ptr        dest = CalcStart(kFirstScrollCol,toRow);
  103.     
  104.     BlockMove(source,dest,kWidth - (kFirstScrollCol-1));
  105. }
  106.  
  107.  
  108. void    ScreenMatrix::ScrollArea()
  109. {
  110.     fInvalidQ = true;
  111.     fSyncCount = 0;
  112.     for(long    row = 2;row <= kBottomScrollRow;row++){
  113.         CopyRow(row,row-1);
  114.     }
  115.     memset(CalcStart(kFirstScrollCol,kBottomScrollRow),' ',(kWidth - (kFirstScrollCol-1)));
  116.  
  117.     fScrollCount++;
  118.     if(fScrollCount > 10){
  119.         RefreshScrollArea();
  120.     }
  121. }    
  122.  
  123.  
  124. long    ScreenMatrix::ScrollAreaHeight()
  125. {
  126.     return kBottomScrollRow;
  127. }
  128.  
  129. Ptr        ScreenMatrix::GetIndRow(long inIndex,long &outSize)
  130. {    
  131.     outSize = kWidth;
  132.     return (Ptr) &fMatrix[inIndex-1][0];
  133.  
  134. }
  135.